import configDB as Mysql_Db
import public_method as pub
import time,datetime
import re
import filelog
v4_sqlMapper=Mysql_Db.Mysql('DATABASE_2')
v5_sqlMapper=Mysql_Db.Mysql('DATABASE_1')
# v4数据库名
old_database = v4_sqlMapper.get_one(' select database() ;')
# v5数据库名
new_database = v5_sqlMapper.get_one(' select database() ;')
filelog.logInfo('v4数据库名:'+str(old_database), 'debug')
filelog.logInfo('v5数据库名:'+str(new_database), 'debug')
# 查询所有已经转换完成的站点数据
filelog.logInfo('获取所有已转换站点数据集合...........', 'debug')
webList=v5_sqlMapper.get_all("select id,new_table,old_table,new_id,old_id from data_conversion_info where new_table='public_web_config' order by id; ")
# 加载所有转换过的用户id信息
filelog.logInfo('获取所有已转换用户数据集合...........', 'debug')
userList=v5_sqlMapper.get_all("select old_id,new_id from data_conversion_info where new_table='public_user';")
# 通过视图将所需转换的v4分类获取
filelog.logInfo('获取v4分类视图数据集合...........', 'debug')
classList=v4_sqlMapper.get_all("select id,name,parentid,ifnull(orderId,0) as orderId,create_user_id, concat(create_time,''),webId,del,classlevel from classes_view where  del=0 order by classlevel asc;")
# v4标签信息集合
filelog.logInfo('获取v4标签信息数据集合...........', 'debug')
lableList=v4_sqlMapper.get_all("select id,label_name,create_user,concat(create_time,''),web_id,del from label where  del=0;")
# 待转化小渠道数据集合
filelog.logInfo('获取v4转化小渠道数据集合...........', 'debug')
channelList=v4_sqlMapper.get_all("select id,channel_id,source_id,name,web_id,add_user_id,concat(add_time,''),update_user_id,concat(update_time,''),chat_title from ch_source_config where  channel_id in(0,1,2,3) and del=0 order by channel_id asc ;")

filelog.logInfo('获取v4转化答案属性数据集合...........', 'debug')
answerChannelList=v4_sqlMapper.get_all("select id,solution_id,sub_solution_id,rule_type,rule_id,webId from answer_config where  rule_type in(1,2,3) and del=0")

# 标准问数据
filelog.logInfo('获取v4所有标准问数据集合...........', 'debug')
questionList=v4_sqlMapper.get_all("SELECT q.id,q.question,q.queLevel,q.add_user_id,concat(q.add_time,''),q.update_user_id,concat(q.update_time,''),q.webId,s.group_id,s.solution_type  "\
                                  " FROM question q  JOIN solution s ON q.id = s.solution_id left join classes c on s.group_id=c.id"
                                  " WHERE s.now_status=0 AND solution_type=1 and s.del = 0 AND q.del = 0 and c.del=0 and c.id is not null;")

# 相似问数据
filelog.logInfo('获取v4所有相似问数据集合...........', 'debug')
sim_questionList=v4_sqlMapper.get_all("SELECT q.id,q.question,q.queLevel,q.add_user_id,concat(q.add_time,''),q.update_user_id,concat(q.update_time,''),q.webId,s.group_id,s.solution_type,q.solution_id  "\
                                  " FROM question q LEFT JOIN solution s ON q.solution_id = s.solution_id left join classes c on s.group_id=c.id"
                                  " WHERE q.queLevel=0 and s.now_status=0 AND s.del = 0 AND q.del = 0 and c.del=0 and c.id is not null;")



# 素材数据
filelog.logInfo('获取v4所有素材数据集合...........', 'debug')
materialList=v4_sqlMapper.get_all("select name,case when type=5 then 0 else type end type,matSize,path,addUserId,time,concat(updateTime,''),webId,id from material where del=0;")

# wx图文数据
filelog.logInfo('获取v4所有wx图文数据集合...........', 'debug')
wxappList=v4_sqlMapper.get_all("select id,addUserId,concat(time,'') as time,subType,webId from wxappmsg where del=0;")

# wx图文详细数据
filelog.logInfo('获取v4所有wx图文详细数据集合...........', 'debug')
wxdetailList=v4_sqlMapper.get_all("select id,appMsgId,title,author,subType,content,imgURL,webId,digest,orderId,showPic from wxappmsg_detail where del=0;")

# 来访者角色数据
filelog.logInfo('获取v4所有来访者角色数据集合...........:', 'debug')
hcCombList=v4_sqlMapper.get_all("select id,name,parent_id,create_user_id,concat(create_time,''),update_user_id,concat(update_time,''),webid,del from hc_comb where name is not null and  del=0 order by id asc;")

# 来访者属性
filelog.logInfo('获取v4所有来访者角色属性数据集合...........:', 'debug')
hcElementList=v4_sqlMapper.get_all("select id,name,parent_id,real_id,if(parent_id!='0',1,0) as type,create_user_id,create_time,update_user_id,update_time,webid,del from hc_element where del=0;")

# 来访者属性组
filelog.logInfo('获取v4所有来访者角色属性数据集合...........:', 'debug')
hcGroupCombList=v4_sqlMapper.get_all("select id,combile_id,element_id,webid from hc_group_comb where del=0;")

# 词库数据 (专业名词、敏感词、停止词、启类词、定制分词、同义词、枚举词、关联词、 词权重)
filelog.logInfo('获取v4所有词库数据 (专业名词、敏感词、停止词、启类词、定制分词、同义词、枚举词、关联词、 词权重等)数据集合...........:', 'debug')
wordList=v4_sqlMapper.get_all("select max(id) id,word,max(ifnull(score,0)) score,tyc,max(ifnull(type,0)) type,wodmode,webId,max(addUserId) addUserId,concat(max(add_time),'') add_time from worddoc  where  wodMode in(1,2,4,5,6,7,8,10,13) and del=0  group by word,webId,tyc,wodmode,webId,ifnull(score,0) union all " \
                            +" select max(id) id,words,0 as score,null as tyc,0 as type, 3 as wodMode, webId,max(addUserId) addUserId,max(concat(time,'')) time from brunchwords where del=0 group by words,webId ;")

filelog.logInfo('建立相关数据存放字典中..........', 'debug')
# 来访者角色新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
combMap=dict()
# wx图文新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
wxappMap=dict()
# 将结果数组转化为字典键值对供后续新旧用户对应使用,K为v4ID值,V为v5对应新ID值
userMap=dict(userList)
# 小渠道新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
sourceMap=dict()
# 存放v4大小渠道对应值K于主键ID的对应V
sourceIdMap=dict()
# 分类新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
classMap=dict()
# 标签新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
lableMap=dict()
# 问题新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
questionMap=dict()
# 问题配置表solution新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
solutionMap=dict()
# 知识点新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
questionMap=dict()
# 素材对应新旧值存放集合,,K为v4ID值,V为v5对应新ID值
materialMap=dict()
# 素材路径对应地址集合,,K为v4ID值,V为path
pathlMap=dict()
# v4path与id对应集合
wxpathMap=dict()
# v5答案组对应k对应知识点,k对应答案组
answGroupMap=dict()
# 答案新老对应map值
answerMap=dict()
# 存放对应词k词汇，v id
wordMap=dict()
# 答案类型集合新旧存放 k为v4，v对应v5值
answerTypeMap=dict()
# 来访者属性
hcElementMap=dict()

# v4文本(0) v5文本(1)
answerTypeMap[0]=1
# v4图文(1) v5图文(3)
answerTypeMap[1]=3
# v4富文本(2) v5富文本(3)
answerTypeMap[2]=2
# v4素材(3) v5素材(4)
answerTypeMap[3]=4
# v4流程(6) v5流程(5)
answerTypeMap[6]=5
# v4转人工(8) v5转人工(6)
answerTypeMap[8]=6

startTime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 通过转换好的站点对站点下的数据进行统一转换 转换路线分类、标签、大小渠道、问题、答案、相似问、流程、寒暄库
filelog.logInfo('数据准备完成,正在进行数据遍历...........', 'debug')
for webOne in webList:
    data_id=webOne[0]
    v5_table=webOne[1]
    v4_table=webOne[2]
    v5_webId=int(webOne[3])
    v4_webId=int(webOne[4])
    mainUid=v5_sqlMapper.get_one("select id from public_user where web_id="+str(v5_webId)+" and type=1;")
    # 记录数据转换条数
    class_num = 0
    lable_num=0
    channel_num=0
    hcComb_num=0
    material_num=0
    wxapp_num=0
    wxDetail_num=0
    channel_num=0
    channel_detailNum=0
    channel_pageNum=0
    channel_wxConfigNum=0
    solution_num=0
    solution_classNum=0
    solution_configNum=0
    question_num=0
    answer_num=0
    answer_channelNum=0
    sim_questionNum=0
    word_num=0
    word_groupNum=0
    word_relationNum=0
    hcElement_num=0
    hcGroup_num=0
    filelog.logInfo('本次扫描方式为站点.....V4站点:'+str(v4_webId)+'V5站点:'+str(v5_webId), 'debug')
    # 分类数据迭代转换
    for classOne in classList:
        class_id=classOne[0]
        class_name=pub.str_symbol(classOne[1])
        class_time=pub.str_symbol(str(classOne[5]))
        class_level=classOne[8]
        #sonUserList[ui][3] if sonUserList[ui][3] != None else ''
        if classOne[6] == v4_webId:
            sqls="INSERT INTO kn_classes(domain_id, name, type, parent_id, order_id,  create_user_id, create_time, update_user_id, update_time,web_id, del)"\
                +"values("+str(5)+","+class_name+","+str(1)+","+str(classOne[2] if classOne[2]==0 else classMap.get(classOne[2]) if classMap.get(classOne[2])!=None else 0)+","+str(classOne[3])+","+str(userMap.get(classOne[4]) if userMap.get(classOne[4]) !=None else mainUid)+","+class_time+","+str(userMap.get(classOne[4]) if userMap.get(classOne[4]) !=None else mainUid)+",SYSDATE(),"+str(v5_webId)+",0);"
            #获取插入数据后的主键ID
            filelog.logInfo('问题分类数据转换-kn_classes.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
            newId=v5_sqlMapper.insert_id(sqls)
            #添加数据更新记录
            v5_sqlMapper.insert(pub.insert_conversion(newId, class_id, 'kn_classes', 'classes', new_database,old_database,v5_webId,v4_webId))
            class_num = class_num + 1
            classMap[class_id]=newId

    # 标签数据迭代转换
    for lableOne in lableList:
        lable_id=lableOne[0]
        lable_name=pub.str_symbol(lableOne[1])
        lable_time=pub.str_symbol(str(lableOne[3]))
        if lableOne[4] == v4_webId:
            sqls="INSERT INTO kn_knowledge_label (name, parent_id, del, web_id, create_user_id, create_time)"\
                +"values("+lable_name+",0,0,"+str(v5_webId)+","+str(userMap.get(lableOne[2]) if userMap.get(lableOne[2]) !=None else mainUid)+","+str(lable_time if lable_time!=None else 'sysdate()')+");"
            # 获取插入数据后的主键ID
            filelog.logInfo('标签数据转换-kn_knowledge_label.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
            newId=v5_sqlMapper.insert_id(sqls)
            # 添加标签数据更新记录
            v5_sqlMapper.insert(pub.insert_conversion(newId, lable_id, 'kn_knowledge_label', 'label', new_database,old_database,v5_webId,v4_webId))
            lable_num = lable_num + 1
            lableMap[lable_id] = newId

    # 来访者角色
    for hcCombOne in hcCombList:
        comb_id=hcCombOne[0]
        comb_name=pub.str_symbol(hcCombOne[1])
        comb_pareId=hcCombOne[2]
        comb_addId=hcCombOne[3]
        comb_addTime=pub.str_symbol(str(hcCombOne[4])) if hcCombOne[4] !=None else 'sysdate()'
        comb_updId=hcCombOne[5] if hcCombOne[5] != None else 'null'
        comb_updTime=pub.str_symbol(str(hcCombOne[6])) if hcCombOne[6] !=None else 'sysdate()'
        comb_webId=hcCombOne[7]
        if comb_webId == v4_webId:
            new_pareId =combMap.get(comb_pareId) if combMap.get(comb_pareId) !=None else '0'
            #来访者角色数据转换
            sqls="insert into public_chatuser_role(name,parent_id,create_user_id,create_time,update_user_id,update_time,web_id,del)" \
                +"values("+comb_name+","+str(new_pareId)+","+str(userMap.get(comb_addId) if userMap.get(comb_addId) != None else mainUid)+","+str(comb_addTime)+","+str(comb_updId)+","+comb_updTime+","+str(v5_webId)+",0)"
            filelog.logInfo('来访者角色数据转换-public_chatuser_role.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
            new_combId=v5_sqlMapper.insert_id(sqls)
            v5_sqlMapper.insert(pub.insert_conversion(new_combId, comb_id, 'public_chatuser_role', 'hc_comb', new_database, old_database,v5_webId,v4_webId))
            hcComb_num=hcComb_num+1
            combMap[comb_id]=new_combId

    # 来访者属性
    for hcElementOne in hcElementList:
        if hcElementOne[9] == v4_webId:
            element_id=pub.str_symbol(hcElementOne[0])
            element_name=pub.str_symbol(hcElementOne[1]) if hcElementOne[1]!=None else 'null'
            element_parent_id=hcElementOne[2]
            element_real_id=hcElementOne[3]
            element_type=hcElementOne[4]
            element_addId=hcElementOne[5]
            element_addTime=pub.str_symbol(str(hcElementOne[6])) if hcElementOne[6] !=None else 'sysdate()'
            element_webId=hcElementOne[9]
            new_element_id=pub.str_symbol(str(v5_webId)+'_'+element_real_id)
            if element_parent_id !='0':
                element_parent_id=pub.str_symbol(str(v5_webId)+'_'+str(element_parent_id[len(str(v4_webId))+1:len(element_parent_id)]))
            # 来访者角色属性转换
            sqls="insert into public_chatuser_role_pro(id,name,parent_id,real_id,type,create_user_id,create_time,web_id,del) " \
                +"values("+new_element_id+","+element_name+","+element_parent_id+","+pub.str_symbol(element_real_id)+","+str(element_type)+","+str(userMap.get(element_addId) if userMap.get(element_addId) != None else mainUid)+","+element_addTime+","+str(v5_webId)+",0)"
            filelog.logInfo('来访者角色数据转换-public_chatuser_role_pro.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
            v5_sqlMapper.insert(sqls)
            v5_sqlMapper.insert(pub.insert_conversion(new_element_id, element_id, 'public_chatuser_role_pro', 'hc_element', new_database, old_database,v5_webId,v4_webId))
            hcElementMap[element_id]=new_element_id
            hcElement_num=hcElement_num+1


    # 来访者关系信息
    for hcGroupCombOne in hcGroupCombList:
        if hcGroupCombOne[3] == v4_webId:
            hcGroupComb_id=hcGroupCombOne[0]
            hcGroupComb_combId=hcGroupCombOne[1]
            hcGroupComb_groupId=pub.str_symbol(hcGroupCombOne[2]) if hcGroupCombOne[2] !=None else 'null'
            hcgroupCombId=combMap.get(hcGroupComb_id)
            hcGroupComb_groupId=hcElementMap.get(hcGroupComb_groupId)
            if hcgroupCombId !=None:
                sqls="insert into public_chatuser_role_pro_relation(chat_role_id,role_pro_id) " \
                    +"values("+str(hcgroupCombId)+","+hcGroupComb_groupId+")"
                filelog.logInfo('来访者角色数据转换-public_chatuser_role_pro_relation.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
                hcGroupCombNewId=v5_sqlMapper.insert_id(sqls)
                v5_sqlMapper.insert(pub.insert_conversion(hcGroupComb_id, hcGroupCombNewId, 'public_chatuser_role_pro_relation', 'hc_group_comb',new_database, old_database, v5_webId, v4_webId))
                hcGroup_num=hcGroup_num+1
    # 素材数据遍历转换,需转移素材文件至v5服务器中
    for materialOne in materialList:
        mater_name = pub.str_symbol(materialOne[0]) if materialOne[0]!=None else 'null'
        mater_type = materialOne[1] if materialOne[1]!=None else 'null'
        mater_size = materialOne[2] if materialOne[2]!=None else 'null'
        mater_path = materialOne[3] if materialOne[3]!=None else 'null'
        mater_addTime = pub.str_symbol(str(materialOne[5])) if materialOne[5] != None else 'sysdate()'
        mater_updTime = pub.str_symbol(str(materialOne[6])) if materialOne[6] != None else 'sysdate()'
        mater_webId = materialOne[7]
        mater_id = materialOne[8]
        if mater_webId == v4_webId:
            # 素材数据转换sql
            sqls = "INSERT INTO public_material(name, type, file_size, path, classes_id, code, use_type, create_user_id, create_time, update_time, web_id)" \
                   + "values(" + mater_name + "," + str(mater_type) + "," + str(mater_size) + "," + pub.str_symbol(mater_path) + ",-1,null,0," + str(userMap.get(materialOne[4]) if userMap.get(materialOne[4]) != None else mainUid) + "," + mater_addTime + "," + mater_updTime + "," + str(v5_webId) + ");"
            filelog.logInfo('素材文件数据转换-public_material.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
            newId = v5_sqlMapper.insert_id(sqls)
            # 添加标签数据更新记录
            v5_sqlMapper.insert(pub.insert_conversion(newId, mater_id, 'public_material', 'material', new_database, old_database,v5_webId,v4_webId))
            material_num=material_num+1
            materialMap[mater_id] = newId
            pathlMap[mater_id] = str('/'+mater_path)
            wxpathMap[mater_path]=mater_id
    # wx图文数据转化 select id,addUserId,time,subType,webId
    for wxappOne in wxappList:
        wxapp_id=wxappOne[0]
        wxapp_addUserId=wxappOne[1]
        wxapp_addUserTime = pub.str_symbol(wxappOne[2]) if wxappOne[2] != None else 'sysdate()'
        wxapp_subType=wxappOne[3]
        wxapp_webId=wxappOne[4]
        if wxapp_webId == v4_webId:
            sqls="INSERT INTO public_image_text(type,hits,create_user_id,create_time,web_id,del,sub_type,classes_id)" \
                +"values(1,0,"+str(userMap.get(wxapp_id) if userMap.get(wxapp_id) !=None else mainUid)+","+wxapp_addUserTime+","+str(wxapp_webId)+",0,"+str(wxapp_subType)+",-1)"
            # VALUES (1,0,'创建ID','创建时间','站点ID',0,'3单图文4多图文','更新时间','更新id',-1)
            filelog.logInfo('wx图文数据转换-public_image_text.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
            newId=v5_sqlMapper.insert_id(sqls)
            v5_sqlMapper.insert(pub.insert_conversion(newId, wxapp_id, 'public_image_text', 'wxappmsg', new_database, old_database,v5_webId,v4_webId))
            wxapp_num=wxapp_num+1
            wxappMap[wxapp_id]=newId
    # wx图文数据明细转化 select id,appMsgId,title,author,subType,content,imgURL,webId,digest,orderId,showPic
    for wxdetailOne in wxdetailList:
        if wxdetailOne[7] == v4_webId:
            wxDetail_id = wxdetailOne[0]
            wxDetail_wxId = wxdetailOne[1]
            wxDetail_title = pub.str_symbols(wxdetailOne[2]) if wxdetailOne[2] != None else 'null'
            wxDetail_author = pub.str_symbols(wxdetailOne[3]) if wxdetailOne[3] != None else 'null'
            wxDetail_subType = wxdetailOne[4]
            wxDetail_content = wxdetailOne[5] if wxdetailOne[5] != None else 'null'
            wxDetail_imgURL = pub.str_symbols(wxdetailOne[6]) if wxdetailOne[6] != None else 'null'
            wxDetail_webId = wxdetailOne[7]
            wxDetail_digest = pub.str_symbols(str(wxdetailOne[8])) if wxdetailOne[8] != None else 'null'
            wxDetail_orderId = wxdetailOne[9] if wxdetailOne[9] != None else 'null'
            wxDetail_showPic = pub.str_symbols(str(wxdetailOne[10])) if wxdetailOne[10] != None else 'null'
            one = wxDetail_imgURL.find("/material/downloadFileById/")
            two =wxDetail_imgURL.find("/upload/web/")+1

            filelog.logInfo('wx图文明细数据转换-URL转换前:' + str(wxDetail_imgURL) , 'debug')
            mateId = None
            if one > 1:
                mateId = int(re.sub("\D", '', str_path))
                new_path = pathlMap.get(mateId) if pathlMap.get(mateId) != None else 'null'

                wxDetail_imgURL = wxDetail_imgURL.replace(str_path, new_path)
            else:
                str_path = str(wxDetail_imgURL[two:len(wxDetail_imgURL) - 1])
                mateId=wxpathMap.get(str_path)
            filelog.logInfo('wx图文明细数据转换-URL转换后:' + str(wxDetail_imgURL), 'debug')
            if mateId == None:
                mateId = 'null'
            wxDetail_content = wxDetail_content.replace('"', '\\"')
            wxDetail_content = wxDetail_content.replace("'", "\\'")
            wxsourceId=v4_sqlMapper.get_one("select 1")
            wxDetail_content=pub.str_symbols(wxDetail_content)
            if wxDetail_webId == v4_webId:
                    sqls = "INSERT INTO public_image_text_details(text_img_id, title, author, text_type, text, path, del, web_id, source_id, summary, order_id, show_text)" \
                       + "values(" + str(wxappMap.get(wxDetail_wxId) if wxappMap.get(wxDetail_wxId) != None else '0') + "," + str(wxDetail_title) + "," + str(wxDetail_author) + ",0," + wxDetail_content + "," + str(wxDetail_imgURL) + ",0," + str(v5_webId) + "," + str(1) + "," + wxDetail_digest + "," + str(wxDetail_orderId) + "," + str(wxDetail_showPic) + ")"
                filelog.logInfo('wx图文明细数据转换-public_image_text_details.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
                newId = v5_sqlMapper.insert_id(sqls)
                v5_sqlMapper.insert(pub.insert_conversion(newId, wxDetail_id, 'public_image_text_details', 'wxappmsg_detail', new_database,old_database,v5_webId,v4_webId))
                wxDetail_num=wxDetail_num+1

    # 大小渠道转化
    for channelOne in channelList:
        if channelOne[4] == v4_webId:
            # 通过站点/大渠道/小渠道拼接构成数据库中唯一渠道标识避免重复
            sourceKey = str(channelOne[4]).join(str(channelOne[1])).join(str(channelOne[2]))
            print(sourceKey)
            # 将每个站点组合而成的渠道值作为K,对应ID为V
            sourceIdMap[int(sourceKey)] = channelOne[0]
            cId = channelOne[0]
            channel_id = channelOne[1]
            channel_name = pub.str_symbol(channelOne[3])
            channel_addtime = pub.str_symbol(str(channelOne[6])) if channelOne[6] != None else 'sysdate()'
            channel_updatetime = pub.str_symbol(str(channelOne[8]))
            print(channelOne[9])
            chat_name = pub.str_symbol(channelOne[9]) if channelOne[9] != None else pub.str_symbol('问答机器人')
            # 预处理字符串问题
            py_ntrance_style = "".join(
                '{"type":1,"picUrl":"/upload/default/window1.png","buttonText":"点我咨询","textColor":"#ffffff","buttonColor":"#079ed8","style":1}')
            py_entrance_position = "".join('{"position":3,"transverse":0,"longitudinal":0}')
            py_window_style = "".join('{"type":1,"style":0,"fontSize":14,"color":"#fff","backgroundPic":null}')
            py_enterprise_info = "".join(
                '{"logoUrl":"/upload/default/robot.png","robotLogo":null,"visitorLogo":null,"name":"数据测试站点","url":null,"remark":null,"pageName":"数据测试站点"}')
            py_window_function = "".join(
                '{"fontFlag":false,"expressionFlag":true,"evaluateFlag":false,"evaluateCloseFlag":true,"answerEvaluateFlag":false,"sendPicFlag":true,"customServiceFlag":false,"reminder":"我已经就位，放马问我吧！","inputBoot":false}')
            py_window_expand_function = "".join('{"quickList":null,"tabFlag":true,"expandFunction":null}')
            # 网页处理数据逻辑
            if channel_id + 1 == 1:
                # 网页小渠道数据转换
                filelog.logInfo('网页小渠道数据转换-ch_channel_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
                sqls="insert into ch_channel_config(channel_id,name,channel_mode,status,create_user_id,create_time,web_id,del)"\
                    +"values("+str(channel_id+1)+","+channel_name+",1,1,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0);"
                newId=v5_sqlMapper.insert_id(sqls)
                v5_sqlMapper.insert(pub.insert_conversion(newId, cId, 'ch_channel_config', 'ch_source_config', new_database, old_database,v5_webId,v4_webId))
                channel_num=channel_num+1
                # 网页小渠道配置信息添加
                filelog.logInfo('网页小渠道明细数据转换-ch_channel_detail.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                sqls="insert into ch_channel_detail(channel_id,rule,rule_type,rule_content,order_id,create_user_id,create_time,web_id,del)"\
                    +"values("+str(newId)+",1,1,"+pub.str_symbol('<p>您好，我是智能客服机器人，请问有什么可以帮您？</p>')+",0,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0),"\
                    +"("+str(newId)+",2,1,"+pub.str_symbol('不好意思，我不是很理解您的问题，请用一句话准确描述')+",1,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0);"
                v5_sqlMapper.insert(sqls)
                channel_detailNum=channel_detailNum+2
                # 网页小渠道页面配置信息添加
                sqls="INSERT INTO ch_page_config(channel_id, robot_name, robot_logo, visitor_logo, title, info, input_word,chat_logo, web_id, del, create_user_id, create_time,update_user_id,update_time, entrance_style,entrance_position,window_style,enterprise_info,window_function,window_expand_function)"\
                    +"values("+str(newId)+","+chat_name+",'/upload/default/robot.png', '/upload/default/visitor.png','数据测试站点', '网页渠道模板','我已经就位，放马问我吧！','/upload/default/robot.png',"+str(v5_webId)+",0,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+",SYSDATE(),"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+",SYSDATE()"\
                    +","+pub.str_symbol(py_ntrance_style)+","+pub.str_symbol(py_entrance_position)+","+pub.str_symbol(py_window_style)+","+pub.str_symbol(py_enterprise_info)+","+pub.str_symbol(py_window_function)+","+pub.str_symbol(py_window_expand_function)+");"

                filelog.logInfo('网页小渠道页面数据转换-ch_channel_detail.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                v5_sqlMapper.insert(sqls)
                channel_pageNum=channel_pageNum+1
                #print("网页渠道处理")

            # 微信处理数据逻辑
            if channel_id + 1 == 2:
                # 微信小渠道数据转换
                sqls="insert into ch_channel_config(channel_id,name,channel_mode,status,create_user_id,create_time,web_id,del)"\
                    +"values("+str(channel_id+1)+","+channel_name+",1,1,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0);"
                filelog.logInfo('微信小渠道数据转换-ch_channel_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                newId = v5_sqlMapper.insert_id(sqls)
                v5_sqlMapper.insert(pub.insert_conversion(newId, cId, 'ch_channel_config', 'ch_source_config', new_database,old_database,v5_webId,v4_webId))
                channel_num=channel_num+1
                # 微信小渠道配置信息添加
                sqls="insert into ch_channel_detail(channel_id,rule,rule_type,rule_content,order_id,create_user_id,create_time,web_id,del)"\
                    +"values("+str(newId)+",1,1,"+pub.str_symbol('您好，我是智能客服机器人，请问有什么可以帮您？')+",0,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0),"\
                    +"("+str(newId)+",2,1,"+pub.str_symbol('不好意思，我不是很理解您的问题，请用一句话准确描述')+",1,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0);"

                filelog.logInfo('微信小渠道明细数据转换-ch_channel_detail.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                v5_sqlMapper.insert(sqls)
                channel_detailNum=channel_detailNum+2
                # 微信渠道配置页信息
                sqls="INSERT INTO ch_wx_config (channel_id, name, app_id, app_secret, token, encoding_aes_key, corp_id, authorizer_refresh_token, type, message_type, encryption_mode, add_style, verify_type_info, create_user_id, create_time, web_id, del)"\
                    +"values("+str(newId)+","+chat_name+","+str(12313131231231313)+","+str(12313131231231313)+","+str(12313131231231313)+","+pub.str_symbol('ada12313131231231313ddqqzzcc111111111111112')+",null,null,0,null,2,0,null,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+",sysdate(), "+str(v5_webId)+",0);"

                filelog.logInfo('微信渠道配置数据转换-ch_wx_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                v5_sqlMapper.insert(sqls)
                channel_wxConfigNum=channel_wxConfigNum+1

            # API处理数据逻辑
            if channel_id + 1 == 3:
                # API小渠道数据转换
                sqls="insert into ch_channel_config(channel_id,name,channel_mode,status,create_user_id,create_time,web_id,del)"\
                    +"values("+str(channel_id+1)+","+channel_name+",1,1,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0);"
                filelog.logInfo('API小渠道数据转换-ch_channel_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
                newId = v5_sqlMapper.insert_id(sqls)
                v5_sqlMapper.insert(pub.insert_conversion(newId, cId, 'ch_channel_config', 'ch_source_config', new_database,old_database,v5_webId,v4_webId))
                channel_num=channel_num+1
                # API小渠道配置明细信息
                sqls="insert into ch_channel_detail(channel_id,rule,rule_type,rule_content,order_id,create_user_id,create_time,web_id,del)"\
                    +"values("+str(newId)+",1,1,"+pub.str_symbol('您好，我是智能客服机器人，请问有什么可以帮您？')+",0,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0),"\
                    +"("+str(newId)+",2,1,"+pub.str_symbol('不好意思，我不是很理解您的问题，请用一句话准确描述')+",1,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0);"
                filelog.logInfo('API小渠道明细数据转换-ch_channel_detail.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                v5_sqlMapper.insert(sqls)
                channel_detailNum=channel_detailNum+2

            # H5处理数据逻辑
            if channel_id + 1 == 4:
                # H5小渠道信息转换
                sqls="insert into ch_channel_config(channel_id,name,channel_mode,status,create_user_id,create_time,web_id,del)"\
                    +"values("+str(channel_id+1)+","+channel_name+",1,1,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0);"

                filelog.logInfo('H5小渠道数据转换-ch_channel_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                newId = v5_sqlMapper.insert_id(sqls)
                v5_sqlMapper.insert(pub.insert_conversion(newId, cId, 'ch_channel_config', 'ch_source_config', new_database,old_database,v5_webId,v4_webId))
                channel_num=channel_num+1
                # H5小渠道明细数据添加
                sqls="insert into ch_channel_detail(channel_id,rule,rule_type,rule_content,order_id,create_user_id,create_time,web_id,del)"\
                    +"values("+str(newId)+",1,1,"+pub.str_symbol('<p>您好,我是智能客服机器人, 我可以回答您相关的业务问题, 有什么问题就问我吧! 很高心为您服务!</p>')+",0,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0),"\
                    +"("+str(newId)+",2,1,"+pub.str_symbol('不好意思，我不是很理解您的问题，请用一句话准确描述')+",1,"+str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) !=None else mainUid)+","+channel_addtime+","+str(v5_webId)+",0);"

                filelog.logInfo('H5小渠道明细数据转换-ch_channel_detail.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                v5_sqlMapper.insert(sqls)
                channel_detailNum=channel_detailNum+2
                # H5小渠道配置也信息添加
                sqls="INSERT INTO ch_page_config(channel_id, robot_name, robot_logo, visitor_logo, title, info, input_word,chat_logo, web_id, del, create_user_id, create_time,update_user_id,update_time, entrance_style,entrance_position,window_style,enterprise_info,window_function,window_expand_function)" \
                    +"values(" + str(newId) + "," + chat_name + ",'/upload/default/robot.png', '/upload/default/visitor.png','数据测试站点', 'h5渠道模板','我已经就位，放马问我吧！','/upload/default/robot.png'," + str(v5_webId) + ",0," + str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) != None else mainUid) + ",SYSDATE()," + str(userMap.get(channelOne[5]) if userMap.get(channelOne[5]) != None else mainUid) + ",SYSDATE()"\
                    +","+pub.str_symbol(py_ntrance_style)+","+pub.str_symbol(py_entrance_position)+","+pub.str_symbol(py_window_style)+","+pub.str_symbol(py_enterprise_info)+","+pub.str_symbol(py_window_function)+","+pub.str_symbol(py_window_expand_function)+");"

                filelog.logInfo('H5小渠道页面配置数据转换-ch_page_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                v5_sqlMapper.insert(sqls)
                channel_pageNum=channel_pageNum+1
            # 添加数据处理map记录信息
            sourceMap[channelOne[0]]=newId


    # 标准问数据转换
    for questionOne in questionList:
        if questionOne[7] == v4_webId:
            question_id=questionOne[0]
            question=questionOne[1]
            que_level=questionOne[2]
            que_add_uid=questionOne[3]
            que_add_time=pub.str_symbol(questionOne[4]) if questionOne[4]!=None else 'sysdate()'
            que_upd_uid=questionOne[5] if questionOne[5]!=None else 'null'
            que_upd_time=pub.str_symbol(questionOne[6]) if questionOne[5]!=None else que_add_time
            que_webId=questionOne[7]
            que_class_id=questionOne[8]
            # v5知识点数据添加对应solution表
            sqls="INSERT INTO kn_knowledge(domain_id, model_id, original_knowledge_id, update_type, version, STATUS, description, dispatch_time, open_to_customer, hits, useful, useless, collection, comment_amount, suggest_amount, start_time, end_time, del,web_id, create_user_id, create_time, update_user_id, update_time, read_expire, open_to_field )" \
                +"values(5,null,null,null,null,1,null,null,null,0,0,0,0,0,0,null,null,0,"+str(v5_webId)+","+str(userMap.get(que_add_uid) if userMap.get(que_add_uid) !=None else mainUid)+","+que_add_time+","+str(userMap.get(que_upd_uid) if userMap.get(que_upd_uid) !=None else mainUid)+","+que_upd_time+",null,null);"
            filelog.logInfo('知识点数据转换-kn_knowledge.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
            newId=v5_sqlMapper.insert_id(sqls)
            filelog.logInfo('知识点数据更新原知识点ID-kn_knowledge.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
            v5_sqlMapper.update("update kn_knowledge  set original_knowledge_id="+str(newId)+" where id="+str(newId))
            v5_sqlMapper.insert(pub.insert_conversion(newId, question_id, 'kn_knowledge', 'solution', new_database, old_database,v5_webId,v4_webId))
            solution_num=solution_num+1
            # 知识点ID对应集合
            solutionMap[question_id]=newId

            # 添加问题分类配置信息, rule_type=2 为分类配置信息
            if que_class_id > 0 and classMap[que_class_id] != None:
                sqls="INSERT INTO kn_knowledge_config(knowledge_id, rule_type, rule_id, web_id)" \
                    +"VALUES("+str(newId)+",2,"+str(classMap[que_class_id])+","+str(v5_webId)+");"
                filelog.logInfo('添加知识对应分类数据-kn_knowledge_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                new_configId=v5_sqlMapper.insert_id(sqls)
                v5_sqlMapper.insert(pub.insert_conversion(new_configId, question_id, 'kn_knowledge_config', 'solution', new_database, old_database,v5_webId,v4_webId))
                solution_classNum=solution_classNum+1

            # 问题标签数据
            filelog.logInfo('v4问题标签数据获取-solution_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
            que_lableList = v4_sqlMapper.get_all("select id,solution_id,rule_id,create_user,create_time,web_id from solution_config where web_id="+str(v4_webId)+" and solution_id="+str(question_id)+" and del=0;")
            # 添加对应问题标签数据
            for que_lableOne in que_lableList:
                queLab_id=que_lableOne[0]
                queLab_queId=que_lableOne[1]
                queRule_id=que_lableOne[2]
                queLab_addId=que_lableOne[3]
                queLab_addTime=que_lableOne[4]
                queWebId=que_lableOne[5]
                if lableMap[queRule_id] != None:
                    sqls="INSERT INTO kn_knowledge_config(knowledge_id, rule_type, rule_id, web_id)" \
                        +"VALUES("+str(newId)+",1,"+str(lableMap[queRule_id])+","+str(v5_webId)+");"
                    filelog.logInfo('添加知识对应标签数据-kn_knowledge_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                    queLab_newId=v5_sqlMapper.insert_id(sqls)
                    v5_sqlMapper.insert(pub.insert_conversion(queLab_newId, queLab_id, 'kn_knowledge_config', 'solution_config',new_database, old_database,v5_webId,v4_webId))
                    solution_configNum=solution_configNum+1
                else:
                    continue
            question = question.replace('"', '\\"')
            question = question.replace("'", "\\'")
            # 添加问题信息
            sqls="INSERT INTO kn_question (question, type, domain_id, knowledge_id, source, hits, useful, useless, del, web_id, create_user_id, create_time, update_user_id, update_time)" \
                +"values("+pub.str_symbols(question)+",1,5,"+str(newId)+",null,0,0,0,0,"+str(v5_webId)+","+str(userMap.get(que_add_uid) if userMap.get(que_add_uid) !=None else mainUid)+","+que_add_time+","+str(userMap.get(que_upd_uid) if userMap.get(que_upd_uid) !=None else mainUid)+","+que_upd_time+")"
            filelog.logInfo('标准问题数据转换-kn_question.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
            insert_que_id=v5_sqlMapper.insert_id(sqls)
            v5_sqlMapper.insert(pub.insert_conversion(insert_que_id, question_id, 'kn_question', 'question', new_database,old_database,v5_webId,v4_webId))
            question_num=question_num+1
            # 查询对应问题答案信息
            sqls="select id,ans_mode,mode_value,solution_id,add_user_id,add_time,update_user_id,update_time,webId,answer from answer where webId="+str(v4_webId)+" and solution_id="+str(question_id)+" and answer_status=0 and del=0 ;"
            filelog.logInfo('获取对应问题答案数据-answer.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
            answerList=v4_sqlMapper.get_all(sqls)
            answer_groupId=None
            # 对应问题答案数据分析
            for answerOne in answerList:
                answer_id=answerOne[0]
                answer_mode=answerOne[1]
                answer_value=answerOne[2] if answerOne[2] != None else 'null'
                answer_sid=answerOne[3]
                answer_addId=answerOne[4]
                answer_addTime=pub.str_symbol(str(answerOne[5])) if answer_addId != None else 'null'
                answer_updId= userMap.get(answerOne[6]) if userMap.get(answerOne[6]) !=None else mainUid if answer_addId != None else 'null'
                answer_updtime=pub.str_symbol(str(answerOne[7])) if answerOne[6] != None else 'null'
                answer_webId=answerOne[8]
                answer_content = answerOne[9] if answerOne[9] != None else 'null'
                nums=str.count(answer_content, '/material/downloadFileById/')
                filelog.logInfo('答案文本替换前数据-answer:' + str(answer_content) , 'debug')
                count=0
                if nums > 0:
                # 循环替换出答案中不合法的图片地址
                    while nums > 0:
                        print(answer_content)
                        count += 1
                        an_one = answer_content.find("/material/downloadFileById/")
                        answer_substr = answer_content[an_one:len(answer_content)]
                        an_two = answer_substr.find('"') + an_one
                        str_path = answer_content[an_one:an_two]
                        mateId = int(re.sub("\D", '', str_path))
                        new_path = pathlMap.get(mateId)
                        #获取至对应素材则替换处理否则忽略
                        if new_path != None:
                            answer_content = answer_content.replace(str_path, new_path)

                        print(str_path, mateId, answer_content)
                        if count == nums:
                            break
                if answer_mode in(0,1,2,3,6,8):
                    answer_mode=answerTypeMap.get(answer_mode)
                else:
                    answer_mode=3
                answer_content = answer_content.replace('"', '\\"')
                answer_content = answer_content.replace("'", "\\'")
                filelog.logInfo('答案文本替换后数据-answer:' + str(answer_content), 'debug')
                sqls="INSERT INTO kn_answer(knowledge_id,type,domain_id,original_answer_id,answer,status,group_id,type_value,del,web_id,create_user_id,create_time,update_user_id,update_time) " \
                    +"values("+str(newId)+","+str(answer_mode)+",5,0,"+pub.str_symbols(answer_content)+",0,0,"+str(wxappMap.get(answer_value) if wxappMap.get(answer_value) != None else 'null')+",0,"+str(v5_webId)+","+str(userMap.get(answer_addId) if userMap.get(answer_addId) !=None else mainUid)+","+answer_addTime+","+str(answer_updId)+","+answer_updtime+")"
                filelog.logInfo('答案数据转换插入-kn_answer.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
                answerId=v5_sqlMapper.insert_id(sqls)
                v5_sqlMapper.insert(pub.insert_conversion(answerId, answer_id, 'kn_answer', 'answer', new_database,old_database,v5_webId,v4_webId))
                answer_num=answer_num+1
                # 答案组Id信息
                if answGroupMap.get(newId) != None:
                    answer_groupId = answGroupMap.get(newId)
                else:
                    answer_groupId=answerId
                    answGroupMap[newId]=answerId
                    filelog.logInfo('答案组数据更新转换插入-kn_answer.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
                    v5_sqlMapper.update("update kn_answer set original_answer_id="+str(answerId)+",group_id="+str(answer_groupId)+" where id="+str(answerId))
                    answerMap[answer_id]=answerId
                # 渠道数据进行转换 solution_id,sub_solution_id,rule_type,rule_id,webId
                filelog.logInfo('答案大小渠道、来访者数据获取-answer_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')

    for answerChannelOne in answerChannelList:
        if v4_webId == answerChannelOne[5]:
            anwChanel_id = answerChannelOne[0]
            anwChanel_sid = answerChannelOne[1]
            anwChanel_aid = answerChannelOne[2]
            anwChanel_type = answerChannelOne[3]
            anwChanel_ruid = answerChannelOne[4]
            anwChanel_webId = answerChannelOne[5]
            new_ruleId = 0
            new_answerId = answerMap.get(anwChanel_aid)
            new_solutionId=solutionMap.get(anwChanel_sid)
            if new_solutionId == None:
                continue
            if new_answerId == None:
                continue
            # 大渠道数据转换只转换固定渠道数据 values('知识点ID','答案ID','规则类型','规则ID','站点ID');

            if anwChanel_type in (1, 2, 3):
                if anwChanel_type == 1:
                    new_ruleId = sourceMap.get(anwChanel_ruid)
                    if sourceMap.get(anwChanel_ruid) == None:
                        continue
                elif anwChanel_type == 2 and combMap.get(anwChanel_ruid) != None:
                    new_ruleId = combMap.get(anwChanel_ruid)
                elif anwChanel_type == 3 and anwChanel_ruid in (0, 1, 2, 3):
                    new_ruleId = anwChanel_ruid + 1
                else:
                    continue
                # if anwChanel_type !=2:
                #  anwChanel_type=1
                if anwChanel_type in(1,3):
                    sqls = "insert into kn_answer_config(knowledge_id,answer_id,rule_type,rule_id,web_id)" \
                           + "values(" + str(new_solutionId) + "," + str(new_answerId) + ",2," + str(new_ruleId) + "," + str(v5_webId) + ")"
                    filelog.logInfo('答案生效渠道、来访者配置数据转换-kn_answer_config.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                if anwChanel_type == 2:
                    sqls = "insert into kn_answer_config(knowledge_id,answer_id,rule_type,rule_id,web_id)" \
                           + "values(" + str(new_solutionId) + "," + str(new_answerId) + ",1," + str(new_ruleId) + "," + str(v5_webId) + ");"
                anwConfigId = v5_sqlMapper.insert_id(sqls)
                v5_sqlMapper.insert(pub.insert_conversion(anwConfigId, anwChanel_id, 'kn_answer_config', 'answer_config', new_database,old_database, v5_webId, v4_webId))
                answer_channelNum = answer_channelNum + 1
            else:
                continue

    # 相似问数据转换
    for sim_questionOne in sim_questionList:
        if sim_questionOne[7] == v4_webId:
            question_id=sim_questionOne[0]
            question=sim_questionOne[1]
            que_level=sim_questionOne[2]
            que_add_uid=sim_questionOne[3]
            que_add_time=pub.str_symbol(sim_questionOne[4]) if sim_questionOne[4]!=None else 'sysdate()'
            que_upd_uid=sim_questionOne[5] if sim_questionOne[5]!=None else 'null'
            que_upd_time=pub.str_symbol(sim_questionOne[6]) if sim_questionOne[5]!=None else 'null'
            que_webId=sim_questionOne[7]
            que_class_id=sim_questionOne[8]
            que_solutionId = sim_questionOne[10]
            questionId=solutionMap.get(que_solutionId)
            question = question.replace('"', '\\"')
            question = question.replace("'", "\\'")
            if questionId != None:
                # 添加问题信息
                sqls="INSERT INTO kn_question (question, type, domain_id, knowledge_id, source, hits, useful, useless, del, web_id, create_user_id, create_time, update_user_id, update_time)" \
                    +"values("+pub.str_symbols(question)+",2,5,"+str(questionId)+",null,0,0,0,0,"+str(v5_webId)+","+str(userMap.get(que_add_uid) if userMap.get(que_add_uid) !=None else mainUid)+","+que_add_time+","+str(userMap.get(que_upd_uid) if userMap.get(que_upd_uid) !=None else mainUid)+","+que_upd_time+")"
                filelog.logInfo('问题相似问数据转换-kn_question.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
                new_simId=v5_sqlMapper.insert_id(sqls)
                v5_sqlMapper.insert(pub.insert_conversion(new_simId, question_id, 'kn_question', 'question_0', new_database, old_database,v5_webId,v4_webId))
                sim_questionNum=sim_questionNum+1
            else:
                continue

    # 词汇数据转换
    for wordOne in wordList:
        word_id=wordOne[0]
        word_content=wordOne[1] if wordOne[1] != None else ''
        word_score=wordOne[2]
        word_tyc=str(wordOne[3]) if wordOne[3] != None else ''
        word_type=wordOne[4]
        word_mode=wordOne[5]
        word_webId=wordOne[6]
        word_addUserId=wordOne[7]
        word_addTime=pub.str_symbol(wordOne[8]) if wordOne[8] != None else 'sysdate()'
        if word_webId == v4_webId:
            # 专业名词(4)、敏感词(10)、停止词(1)、启类词(13)、定制分词(8)、同义词(2)、枚举词(7)、关联词(6)、 词权重(5)、主干词(3)
            # 专业名词
            if word_mode == 4:
                sqls="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                    +"values("+pub.str_symbol(word_content[0:100])+","+str(word_score)+",0,1,0,0,0,"+str(v5_webId)+",0,"+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) !=None else mainUid)+","+word_addTime+")"

            # 敏感词
            if word_mode == 10:
                sqls="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                    +"values("+pub.str_symbol(word_content[0:100])+","+str(word_score)+",0,2,0,0,0,"+str(v5_webId)+",0,"+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) !=None else mainUid)+","+word_addTime+")"

            # 停止词
            if word_mode == 1:
                sqls="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                    +"values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,3,0,0,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            # 启类词
            if word_mode == 13:
                sqls ="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                     +"values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,1,0,0,1," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            # 主干词
            if word_mode == 3:
                sqls ="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                     +"values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,1,0,1,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            # 同义词
            if word_mode == 2:
                sqls ="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                     +"values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,1,0,0,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            # 枚举词
            if word_mode == 7:
                sqls = "insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                     + "values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,1,0,0,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            # 关联词
            if word_mode == 6 and len(word_tyc) > 0:

                word_tyc1 = str(word_tyc).split(',')
                for w in range(len(word_tyc1)):
                    if w > 0:
                        break
                    wstr=word_tyc1[w]
                    # 防止数据空
                    if wstr == None and w == 0:
                        w=w-1
                        continue
                    sqls ="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                         +"values(" + pub.str_symbol(wstr[0:100]) + ",0,0,1,0,0,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"
            # 词权重
            if word_mode == 5:
                sqls = "insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                       + "values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,1,0,0,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            if word_content != None and  word_mode not in(1,10):
                wordKey=pub.str_symbol(word_content[0:100] + str(1)+str(v4_webId))
                if wordMap.get(wordKey) == None:
                    # 将处理好的sql语句进行执行
                    filelog.logInfo('主要词汇数据转换-kn_word_doc.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                    new_wordId = v5_sqlMapper.insert_id(sqls)
                    wordMap[wordKey]=new_wordId
                    v5_sqlMapper.insert(pub.insert_conversion(new_wordId, word_id, 'kn_word_doc', 'worddoc', new_database,old_database,v5_webId,v4_webId))
                    word_num=word_num+1

            # 判断是否存在子句
            if  len(word_tyc) > 0:
                word_tyc = str(word_tyc).split(',')
                # 循环添加副词
                for w in range(len(word_tyc)):
                    words=word_tyc[w]
                    # 处理同义词
                    if words != None and word_mode == 2:
                        sql_s="insert into kn_word_relation(word_id,word,type,multi_dimension_id,web_id,create_user_id,create_time) " \
                             +"values("+str(new_wordId)+","+pub.str_symbol(words[0:100])+",1,0,"+str(v5_webId)+","+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime +")"

                    # 处理关联词
                    elif words != None and word_mode == 6:
                        # 对第一个单词做出处理动作
                        if w == 0:
                            sql_str = "insert into kn_word_group(weight,web_id,del,create_user_id,create_time) " \
                                    + "values("+str(word_score)+","+str(v5_webId)+",0,"+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid)+","+word_addTime+")"
                            filelog.logInfo('主要词汇关联词数据转换-kn_word_group.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
                            new_wordGroupId=v5_sqlMapper.insert_id(sql_str)
                            v5_sqlMapper.insert(pub.insert_conversion(new_wordGroupId, word_id, 'kn_word_group', 'worddoc',new_database, old_database,v5_webId,v4_webId))
                            word_groupNum=word_groupNum+1

                            sql_s = "insert into kn_word_relation(word_id,word,type,multi_dimension_id,web_id,create_user_id,create_time) " \
                                  + "values("+str(new_wordGroupId)+","+pub.str_symbol(wstr[0:100])+",4,0,"+str(v5_webId)+","+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime +")"
                        else:
                            sql_s = "insert into kn_word_relation(word_id,word,type,multi_dimension_id,web_id,create_user_id,create_time) " \
                                      + "values(" + str(new_wordGroupId) + "," + pub.str_symbol(words[0:100]) + ",4,0," + str(v5_webId)+"," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

                    elif words != None and word_mode == 7:
                        sql_s="insert into kn_word_relation(word_id,word,type,multi_dimension_id,web_id,create_user_id,create_time) " \
                             +"values("+str(new_wordId)+","+pub.str_symbol(words[0:100])+",3,0,"+str(v5_webId)+","+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime +")"

                    else:
                        continue
                    filelog.logInfo('词汇关系数据转换-kn_word_relation.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId),'debug')
                    new_relationId = v5_sqlMapper.insert_id(sql_s)
                    v5_sqlMapper.insert(pub.insert_conversion(new_relationId, word_id, 'kn_word_relation', 'worddoc', new_database,old_database,v5_webId,v4_webId))
                    word_relationNum=word_relationNum+1

    filelog.logInfo('添加数据转换条数记录-data_tables_info.....V4站点:' + str(v4_webId) + 'V5站点:' + str(v5_webId), 'debug')
    v5_sqlMapper.insert(pub.insert_tables('kn_classes', 'classes', new_database, old_database, class_num, v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_knowledge_label', 'label', new_database, old_database, lable_num, v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('public_chatuser_role', 'hc_comb', new_database, old_database, hcComb_num, v5_webId,v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('public_material', 'material', new_database, old_database, material_num, v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('public_image_text', 'wxappmsg', new_database, old_database, wxapp_num, v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('public_image_text_details', 'wxappmsg_detail', new_database, old_database, wxDetail_num,v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('ch_channel_config', 'ch_source_config', new_database, old_database, channel_num, v5_webId,v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('ch_channel_detail', 'ch_source_config', new_database, old_database, channel_detailNum,v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('ch_page_config', 'ch_source_config', new_database, old_database, channel_pageNum, v5_webId,v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('ch_wx_config', 'ch_source_config', new_database, old_database, channel_wxConfigNum, v5_webId,v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_knowledge', 'solution', new_database, old_database, solution_num, v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_knowledge_config', 'solution', new_database, old_database, solution_classNum, v5_webId,v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_knowledge_config', 'solution_config', new_database, old_database, solution_configNum,v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_question', 'question_1', new_database, old_database, question_num, v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_answer', 'answer', new_database, old_database, answer_num, v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_answer_config', 'answer_config', new_database, old_database, answer_channelNum, v5_webId,v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_question', 'question_0', new_database, old_database, sim_questionNum, v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_word_doc', 'worddoc', new_database, old_database, word_num, v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_word_group', 'worddoc', new_database, old_database, word_groupNum, v5_webId, v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('kn_word_relation', 'worddoc', new_database, old_database, word_relationNum, v5_webId,v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('public_chatuser_role_pro', 'hc_element', new_database, old_database, hcElement_num, v5_webId,v4_webId))
    v5_sqlMapper.insert(pub.insert_tables('public_chatuser_role_pro_relation', 'hc_group_comb', new_database, old_database, hcGroup_num, v5_webId,v4_webId))
endTime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
v5_sqlMapper.dispose()
v5_sqlMapper=Mysql_Db.Mysql('DATABASE_1')
tableNum_List=v5_sqlMapper.get_all("select new_table,old_table,new_webId,old_webId,count(1) as num from data_conversion_info group by new_table,old_table,new_webId,old_webId;")
# 输出本次转换结果
for tableNumsOne in tableNum_List:
    filelog.logInfo(tableNumsOne[0]+'表,v4-'+str(tableNumsOne[3])+'站点成功导入'+str(tableNumsOne[4])+'数据...','info')
filelog.logInfo('本次数据转换开始于:'+str(startTime)+'终止于:'+str(endTime),'info')
